package 剑指offer;

import 树.TreeNode;

/**
 * 输入两棵二叉树A，B，判断B是不是A的子结构。（ps：我们约定空树不是任意一个树的子结构）
 * Created by yang on 2016/11/20.
 * 1. 先使用前序，中序，后续遍历
 * 2. 再判断子串
 */
public class TreeIsCom {

    public boolean HasSubtree(TreeNode root1,TreeNode root2) {
        StringBuilder stringBuilder1 = new StringBuilder();
        StringBuilder stringBuilder2 = new StringBuilder();
        preTraverse(root1,stringBuilder1);
        preTraverse(root2,stringBuilder2);
        System.out.println(stringBuilder1.toString());
        System.out.println(stringBuilder2.toString());
        int index = stringBuilder1.toString().indexOf(stringBuilder2.toString());
        if (index != -1) {
            return true;
        }
        return false;
    }

    public void preTraverse(TreeNode treeNode, StringBuilder stringBuilder) {

        if (treeNode != null) {
            stringBuilder.append(String.valueOf(treeNode.val));
            preTraverse(treeNode.lChild, stringBuilder);
            preTraverse(treeNode.rChild, stringBuilder);
        }
    }

    public void StringJoin(String str) {
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append(str);
    }
}